// pages/fare/changeFare.js
var app = getApp();
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    taping:false,//按钮是否可以点击的依据
    activeIndex:-1,//被激活的按钮的下标
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user_info = wx.getStorageSync('user_info');
    if (user_info) {
      this.setData({
        user_info: user_info,
      })
      if (user_info.nickname) {
        var hasLogin = true;
      } else {
        var hasLogin = false;
      }
      this.setData({
        hasLogin: hasLogin
      })
    }
    this.getList();
  },
  phoneInput:function(e){
    this.setData({
      phone:e.detail.value,
    })
  },
  // 授权事件
  getUserInfoFun: function (e) {
    if (e.detail.errMsg !== "getUserInfo:ok") {
      return;
    }
    var that = this;
    var iv = e.detail.iv,
      encryptedData = e.detail.encryptedData;
    app.authuorLogin(iv, encryptedData).then(function (data) {
      console.log(data);
      if (data.nickname) {
        var hasLogin = true;
      }
      if (data.is_tan == 1) {
        that.setData({
          showhongbao: true,
          gift_fare: { price: data.price }
        })
      }
      that.setData({
        user_info: data.nickname ? data : "",
        hasLogin: hasLogin ? hasLogin : false,
      })
    })
  },
  // 获取话费种类列表
  getList:function(){
    var that = this,
      url = 'fare/index',
      dataObj = {};
    app.request(url,dataObj,function(res){
      console.log(res);
      that.setData({
        list:res.data.data.list,
        tel_fare:res.data.data.tel_fare
      })
    })
  },
  tap:function(e){
    var that = this;
    if(!this.data.taping){
      var id = e.currentTarget.dataset.id,
        index = e.currentTarget.dataset.index,
        price = e.currentTarget.dataset.price;
      // 防止用户重复点击
      this.setData({
        taping: true,
        activeIndex: index
      })
      var reg = /^1[345789]{1}\d{9}$/;
      if (!reg.test(that.data.phone)){
        var title = '请检查手机号码';
        wx.showToast({
          title: title,
          icon: 'none'
        })
        // 恢复可点击状态
        this.setData({
          taping: false,
          activeIndex: -1
        })
        return 
      }
      var url = app.globalData.appUrl +'fare/onlineOrder1';
      wx.showModal({
        title: '确定充值',
        content: '是否充值'+price+'元到手机号：'+that.data.phone,
        success:function(res){
          if(res.confirm){
            var timestamp = parseInt(new Date().getTime()/1000);
            wx.request({
              url: url,
              data: {
                product_id: id,
                timestamp: timestamp,
                tel: that.data.phone,
                uid: wx.getStorageSync('uid'),
                sign: util.sign(Object({ 'product_id': id, 'tel': that.data.phone, 'timestamp': timestamp, 'uid': wx.getStorageSync('uid') }),wx.getStorageSync('user_info').open_id)
              },
              success:function(res){
                console.log(res.data.errno)
                if(res.data.errno==0){
                  that.setData({
                    tel_fare: (Number(that.data.tel_fare) - Number(price)).toFixed(2),
                    tankuangShow: true,//0819版本更改为显示弹框
                    tankuangTitle: '充值成功',
                    tankuangContent: '恭喜你充值成功，充值到账会有延迟，请以收到短信为准，分享好友一起来抽奖吧',
                    btnId: 7,
                  })
                }else{
                  wx.showToast({
                    title: res.data.errmsg,
                    icon:'none',
                  })
                  // 恢复可点击状态
                  that.setData({
                    taping: false,
                    activeIndex: -1
                  })
                }
              },
              complete:function(res){
                // 恢复可点击状态
                that.setData({
                  taping: false,
                  activeIndex: -1
                })
              }
            })
          }else{
            // 恢复可点击状态
            that.setData({
              taping: false,
              activeIndex: -1
            })
          }
        }
      })
      
    }
    
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    var that = this;
    var id = e.from === 'button' ? e.target.id : 1;
    var title, imageUrl;
    console.log(id);
    title = app.shareConfig['' + id].title,
      imageUrl = app.shareConfig['' + id].img_url;

    return {
      title: title,
      path: '/pages/index/index?parent_id=' + wx.getStorageSync('uid'),
      imageUrl: imageUrl,
      success: function (res) {
        if (res.errMsg == 'shareAppMessage:ok' && that.data.tankuangShow) {
          that.setData({
            tankuangShow: false
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    }
  },
  // 关闭弹框
  tankuangclose: function () {
    this.setData({
      tankuangShow: false
    })
  }
})